<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\AdvType;
use App\Models\AdminRoleUser;
use App\Models\AdminUsers;
use App\Models\Reservation;
use App\Models\ReservationServiceCategory;
use App\Models\SystemRegion;
use App\Models\User;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Models\Administrator as AdministratorModel;
use Dcat\Admin\Models\Repositories\Administrator;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Widgets\Alert;

class VillageLeaderController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->header('乡村振兴员管理')
            ->description('列表')
            ->breadcrumb('乡村振兴员管理')
            ->body(function (Row $row) {
                $set_expert_intro = '1. 管理员在"系统"->"管理员"菜单中添加"乡村振兴管理员"角色管理员；<br>
                                     2. 在当前页面中选择相应的管理员，点击"立即关联"选择相应的前台用户关联即可完成乡村振兴员设置<br>';
                $row->column(12, Alert::make($set_expert_intro, '乡村振兴员设置说明')->info());
                $row->column(12, $this->grid());

        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Administrator('roles'), function (Grid $grid) {
            // 获得所有专家管理员
            $expert_admin = AdminRoleUser::where('role_id', 2)->pluck('user_id');
            $grid->model()->whereIn('id', $expert_admin);

            $grid->column('id', 'ID')->sortable();
            $grid->column('username', '管理员用户名');
            $grid->column('name', '管理员名称');
            $grid->column('roles')->pluck('name')->label('primary', 3);
            $grid->column('region_id', '所属地区')->display(function ($region_id) {
                $region = SystemRegion::find($region_id);
                return $region ? $region->region_name : '';
            });

            $grid->column('user_state', '关联状态')->display(function () {
                if ($this->user_id > 0) {
                    $html = '<div class="text-green">已关联</div>';
                } else {
                    $html = '<div class="text-red">待关联</div>';
                }
                return $html;
            });

            $grid->column('user_id', '关联注册用户')->display(function ($user_id) {
                $user_name = '';
                if ($user_id > 0) {
                    $user_info = User::find($user_id);
                    $user_name = $user_info->real_name ?? $user_info->user_name;
                }
                return $user_name;
            });

            $grid->column('opt', '操作')->display(function () {
                $btn_title = $this->user_id > 0 ? '编辑' : '立即关联';
                $edit_url = admin_base_path('village_leader/'.$this->id.'/edit');
                $btn_html = "<a class='btn-sm btn-primary' href='{$edit_url}' style='margin-right: 10px;'>
                            {$btn_title}
                         </a>";
                return $btn_html;
            });

            $grid->quickSearch(['id', 'name', 'username']);
            $grid->disableActions();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {

    }

    public function edit($id, Content $content)
    {
        return $content
            ->title('振兴员管理')
            ->description('编辑')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        return Form::make(new Administrator('roles'), function (Form $form) use ($id) {
            $region_id = 0;
            $region_code = 0;
            $admin_user = AdminUsers::find($id);
            if ($admin_user) {
                $region_id = $admin_user->region_id;
                if ($region_id) {
                    $region = SystemRegion::find($region_id);
                    $region_code = $region->region_code;

                    $region_html = '地区：' . $region->region_name;
                    $form->html(Alert::make($region_html)->info());
                }
            }

            $form->display('id');
            $form->hidden('id');
            $form->text('username', '管理员用户名')->disable();
            $form->text('name', '管理员名称')->disable();

            $form->selectTable('user_id', '用户选择')
                ->title('用户列表')
                ->from(UserTable::make(['region_code' => $region_code]))
                ->model(\App\Models\User::class, 'id', 'user_name')
                ->placeholder('请点击右侧箭头选择注册用户');
//                ->value($obj_id1);


            $form->tools(function (Form\Tools $tools) {
                // 去掉跳转详情页按钮
                $tools->disableView();
                // 去掉删除按钮
                $tools->disableDelete();
            });

            $form->disableViewCheck();
            $form->disableEditingCheck();
            $form->disableCreatingCheck();

            $form->saving(function (Form $form) {
                $admin_user = AdminUsers::find($form->id);
                if ($admin_user) {
                    if ($admin_user->user_id != $form->user_id) {
                        $user = User::find($admin_user->user_id);
                        if ($user) {
                            $user->role = 0; // 设置为普通用户
                            $user->service_catid = 0;
                            $user->save();
                        }
                    }
                }
            });

            $form->saved(function (Form $form, $result) {
                $user = User::find($form->user_id);
                if ($user) {
                    $user->role = 1; // 用户表设置乡村振兴员
                    $user->save();
                }
            });


        });
    }
}
